package com.latefly.skewwork.ui.page.tools

import android.content.Intent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.latefly.skewwork.data.consts.PageIndex
import com.latefly.skewwork.ui.layout.LayoutViewModel
import com.latefly.skewwork.ui.layout.MainLayout
import com.latefly.skewwork.ui.page.tools.camera.CameraActivity
import androidx.activity.compose.rememberLauncherForActivityResult

@Composable
fun ToolsPage(
    navController: NavHostController,
    modifier: Modifier,
    layoutViewModel: LayoutViewModel
) {
    MainLayout(
        navController = navController,
        content = {
            Tools(navController)
        },
        layoutViewModel = layoutViewModel
    )
}

@Composable
fun Tools(
    navController: NavHostController,
    modifier: Modifier = Modifier
) {
    Layout(
        modifier = modifier,
        content = {
            val appList = listOf<PageIndex>(
                PageIndex.Test,
                PageIndex.TodoList,
                PageIndex.BLE,
                PageIndex.Calculator,
                PageIndex.Camera,

                )
            val context = LocalContext.current
            //ToDo 跳转Activity
            val launchDetailScreen = rememberLauncherForActivityResult(
                contract = ActivityResultContracts.StartActivityForResult()
            ) { intent ->
                // 可以在这里根据需要修改intent
                Intent(context, CameraActivity::class.java).apply {
                    putExtra("key", "value") // 设置Activity需要的额外数据
                }
            }

            for (item in appList) {
                IconButton(
                    onClick = {
                        if (item.startActivity) {
                            launchDetailScreen.launch(Intent())
                        } else {
                            navController.navigate(item.router)
                        }

                    },
                    modifier = Modifier
                        .height(80.dp)
                        .width(80.dp)
                ) {
                    Column {
                        Icon(
                            imageVector = item.imageVector,
                            tint = LocalContentColor.current.copy(alpha = 0.5f),
                            contentDescription = stringResource(id = item.description)
                        )
                        Text(text = stringResource(id = item.description))
                    }


                }
            }
        }
    ) { measurables, constraints ->
        val placeables = measurables.map { measurable ->
            measurable.measure(constraints)
        }

        layout(constraints.maxWidth, constraints.maxHeight) {

            val margin = 100
            val xSpan = (constraints.maxWidth - margin / 2) / 4
            var yPosition = 0
            var xPosition = xSpan + margin / 4

            placeables.forEachIndexed { index, placeable ->
                placeable.placeRelative(x = xPosition - placeable.width / 2, y = yPosition)

                if ((index + 1) % 3 == 0) {
                    yPosition += placeable.height
                    xPosition = xSpan + margin / 4
                } else {
                    xPosition += xSpan
                }

            }
        }
    }
}